<?php

declare(strict_types=1);

namespace App\Controller\playlet;
use App\Controller\BaseController;
use App\Model\AppletPlayletRel;
use App\Model\Playlet;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\HttpServer\Contract\ResponseInterface;
use Hyperf\HttpServer\Annotation\AutoController;
use Hyperf\Di\Annotation\Inject;
use App\Model\Applet;
use App\Common\Response\SystemCode;
use App\Service\playlet\AppletService;

#[AutoController]
class AppletController  extends BaseController
{

    #[Inject]
    private AppletService $appletService;
    /**
     * 小程序列表
     */
    public function Index(RequestInterface $request)
    {

        $data = $this->appletService->search($request->query());

        return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功',$data);


    }
    /**
     * 小程序详情
     */
    public function View(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $appletInfo = Applet::query()->select('id', 'name','cover','app_id','app_secret','token','user_default_headimg','coin_name','static_base_url', 'status')->where('id', $id)->first();
            if (empty($appletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }
            $appletInfo['status_name'] = self::$commonStatus[$appletInfo->status];
            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $appletInfo);
        }
    }
    /**
     * 小程序创建
     */
    public function Create(RequestInterface $request){

        $result = $this->appletService->createOrUpdate($request->post());

        if($result['code'] == SystemCode::SYSTEM_SUCCESS){
            return $this->response(SystemCode::SYSTEM_SUCCESS, '创建成功');
        }else{
            return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

        }
    }
    /**
     * 小程序删除
     */
    public function Delete(int $id,RequestInterface $request)
    {
        if(!empty($id)) {
            $AppletInfo = Applet::query()->where('id', $id)->first();
            if (empty($AppletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }
            $AppletInfo->deleted_at = date('Y-m-d H:i:s');
            $AppletInfo->save();
            return $this->response(SystemCode::SYSTEM_SUCCESS, '操作成功');
        }
    }
    /**
     * 小程序更新
     */
    public function Update(int $id,RequestInterface $request){

        if(empty($id)){
            return $this->response(SystemCode::SYSTEM_ERROR, '错误请求');
        }else{
            $appletInfo = Applet::query()->where('id',$id)->first();
            if(empty($appletInfo)){
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }
            if($request->getMethod() == 'GET'){
                return $this->response(SystemCode::SYSTEM_SUCCESS, '查询成功',$appletInfo);

            }else{
                $result = $this->appletService->createOrUpdate($request->post(),true,$appletInfo);

                if($result['code'] == SystemCode::SYSTEM_SUCCESS){
                    return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
                }else{
                    return $this->response(SystemCode::SYSTEM_ERROR, $result['message']);

                }

            }
        }
    }


    public function bindPlaylet(int $id,RequestInterface $request){

        if(!empty($id)) {
            $appletInfo = Applet::query()->select('id')->where('id', $id)->first();
            if (empty($appletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }


            if($request->getMethod() == 'GET'){
                $playletIds = AppletPlayletRel::query()->where(['applet_id' => $id])->pluck('playlet_id');

//                $playletIds = Playlet::query()->whereIn('id',$playletIds)->pluck('playlet_id');

                return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletIds);

            }else{
                //删除之前绑定得
                $playletIds = $request->post('playlet_ids');

                AppletPlayletRel::query()->where(['applet_id' => $id])->delete();
                if(!empty($playletIds) && is_array($playletIds)){
                    $playletIds = Playlet::query()->whereIn('id',$playletIds)->pluck('id');
                    foreach ($playletIds as $playlet_id){
                        AppletPlayletRel::create(
                            [
                                'applet_id'   => $id,
                                'playlet_id'   => $playlet_id,
                            ]
                        );
                    }
                }
                return $this->response(SystemCode::SYSTEM_SUCCESS, '修改成功');
            }
        }

    }

    public function playletList(int $id,RequestInterface $request){

        if(!empty($id)) {
            $appletInfo = Applet::query()->select('id')->where('id', $id)->first();
            if (empty($appletInfo)) {
                return $this->response(SystemCode::SYSTEM_ERROR, '小程序不存在');
            }

            $playletIds = AppletPlayletRel::query()->where(['applet_id' => $id])->pluck('playlet_id');

            $playletData = Playlet::query()->whereIn('id',$playletIds)->select(['id as value','name as label'])->get();

            return $this->response(SystemCode::SYSTEM_SUCCESS, '请求成功', $playletData);

        }

        return $this->response(SystemCode::SYSTEM_ERROR, '小程序没有绑定的短剧');

    }
}